package com.cheer.controller;

import cn.hutool.poi.excel.ExcelUtil;
import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnEntryQueryDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaLearnEntryService;
import com.cheer.vo.OaLearnEntryCompareVO;
import com.cheer.vo.OaLearnEntryDetailVO;
import com.cheer.vo.OaLearnEntryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/learn/entry")
@Api(tags = "报名路由")
public class OaLearnEntryController {

    @Autowired
    private OaLearnEntryService oaLearnEntryService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @ApiOperation("查询学习报名列表")
    @GetMapping("/list")
    public Result<Page<OaLearnEntryVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                             @RequestParam Integer learnId,
                                             Integer pid) {
        if (pid == null) {
            pid = userInfoHolder.getAdmin().getPid();
        }
        return Result.success(oaLearnEntryService.list(pageNum, pageSize, learnId, pid));
    }

    @ApiOperation("下载学习报名列表")
    @PostMapping("/download")
    public Result download(@RequestParam Integer learnId,
                           Integer pid, HttpServletResponse response) {
        if (pid == null) {
            pid = userInfoHolder.getAdmin().getPid();
        }
        oaLearnEntryService.download(learnId, pid, response);
        return Result.success();
    }

    @ApiOperation("查询学习报名详情")
    @GetMapping("/detail")
    public Result<Page<OaLearnEntryDetailVO>> detail(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam Integer learnId,
                                                     @RequestParam Integer pid) {

        return Result.success(oaLearnEntryService.detail(pageNum, pageSize, learnId, pid));
    }

    @ApiOperation("下载学习报名详情")
    @PostMapping("/detail/download")
    public Result detailDownload(@RequestParam Integer learnId, @RequestParam Integer pid, HttpServletResponse response) {
        oaLearnEntryService.detailDownload(learnId, pid, response);
        return Result.success();
    }

    @ApiOperation("查询学习报名对比")
    @PostMapping("/compare")
    public Result<OaLearnEntryCompareVO> compare(@RequestBody @Valid OaLearnEntryQueryDTO learnEntryQueryDTO) {
        if (learnEntryQueryDTO.getPid() == null) {
            learnEntryQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        return Result.success(oaLearnEntryService.compare(learnEntryQueryDTO));
    }


    @ApiOperation(value = "完成率导出", produces = "application/octet-stream")
    @PostMapping("/percentageExport")
    public void percentageExport(@RequestBody @Valid OaLearnEntryQueryDTO learnEntryQueryDTO) {
        if (learnEntryQueryDTO.getPid() == null) {
            learnEntryQueryDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        OaLearnEntryCompareVO compare = oaLearnEntryService.compare(learnEntryQueryDTO);
        oaLearnEntryService.drawExcel(compare);
    }


}
